<?php
class Answer_Component_Controller_Admincp_Edit extends Phpfox_Component {
    public function process() {
        $sLink = "admincp.answer.category";
        if ($iEditId = $this->request()->getInt('id'))
        {
            $aConditions=array('id = '. $iEditId);
            if ($aCategory = Phpfox::getService('answer.category')->getForEdit($aConditions))
            {
                $this->template()->assign('aForms', $aCategory['data']);
            }
        }
        if  ( $aVals = $this->request()->getArray('val')) {
            Phpfox::getService('answer.category')->saveDatabase($aVals);
            $this->url()->send($sLink, array(), Phpfox::getPhrase('answer.category_updated_successfully'));
        }

        $aConditions = array('c.id <> '. $this->request()->getInt('id'));
        $aCategories = Phpfox::getService('answer.category')->getListNoPage('c.*',$aConditions);
        $aCategoriesCombo = $aCategories['data'];

        $aCondition = array(

            'c.parent_id = 0'
        );
        $aCategories = Phpfox::getService('answer.category')->getListNoPage('c.*', $aCondition);
        $aCategories = $aCategories['data'];
        foreach ($aCategories as $iKey => $aVal) {
            $aCondition = array(
                'c.parent_id = ' . $aVal['id']
            );
            $aSubCategories = Phpfox::getService('answer.category')->getListNoPage('c.*', $aCondition);
            $aCategories[$iKey]['child'] = $aSubCategories['data'];
        }

        $this->template()
            ->setTitle(Phpfox::getPhrase('answer.manager_categories'))
            ->setBreadcrumb(Phpfox::getPhrase('answer.edit_category'))
            ->setHeader(array(
            'admin.js' => 'module_answer',
            '<script type="text/javascript">$Core.answers.url(\'' . $this->url()->makeUrl('admincp.answer') . '\');</script>'
            ))
            ->assign('aCategoriesCombo',$aCategoriesCombo);
    }
}